/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var roles="";
var userExists;
var page;

function addUser(){    
//    alert("start addUser");
    if(page=="admin"){
        fillRoles();
    }
    else{
        if(page=="registration")
            roles="";
    }
//    alert("start post");
    $.post("AddUser",{
        Login:document.getElementById("Login").value,
        First_Name:document.getElementById("First_Name").value,
        Last_Name:document.getElementById("Last_Name").value,
        E_mail:document.getElementById("E_mail").value,
        Address:document.getElementById("Address").value,
        Password:document.getElementById("Password").value,
        Roles:roles
    },
    function(result){
        roles="";
        alert("finish addUser");
//        alert(page)
        if(page=="admin"){
            window.location.href="admin.jsp";
        }
        if(page=="registration"){
            window.location.href="main.jsp";
        }
    })
}

function fillRoles(){
//    alert("fillRoles");
    if (document.getElementById("Admin").checked){
        addComma();
        roles+="Administrator";
    }
    if (document.getElementById("Warehouse_Employee").checked) {
        addComma();
        roles+="Warehouse Employee";
    }
    if (document.getElementById("Courier").checked){
        addComma();
        roles+="Courier";
    }
    if (document.getElementById("Customer").checked){
        addComma();
        roles+="Customer";
    }
    console.log(roles);
//    alert("finish fillRoles");
}

function addComma(){
    if(roles!="")
        roles+=",";
}

function checkFields(){
    alert("start check fields");
    if (
        document.getElementById("Login").value!="" &&
        document.getElementById("First_Name").value!="" &&
        document.getElementById("Last_Name").value!="" &&
        document.getElementById("E_mail").value!="" &&
        document.getElementById("Address").value!="" &&
        document.getElementById("Password").value!="" &&
        document.getElementById("Confirmation").value!="" &&
        userExists==false
            )
        if(document.getElementById("Password").value==document.getElementById("Confirmation").value){
            alert("everything okay");
            document.getElementById('warningConfirm').innerHTML = "";
            document.getElementById('warning').innerHTML = "";
            addUser();
            return true;
        }
        else{
            document.getElementById('warningConfirm').innerHTML = 'Wrong confirmation';
            return false;
        }
        
    else {
        document.getElementById('warning').innerHTML = 'All fields must be filled';
        return false;
    }
    alert("finish check fields");
}

function checkLogin(){
    alert("start check login");
    $.post("CheckLogin", {
        Login:document.getElementById("Login").value
    }, function(result){
        if (result=="false"){
            document.getElementById('warningLogin').innerHTML = 'User with such login already exists';
            userExists=true;
            alert("fail check");
        }
        else{
            document.getElementById('warningLogin').innerHTML = '';
            userExists=false;
            alert("success check");
        }
    })
    alert("finish check login");
}

$(function() {
    $(document).on('submit', '.js-adduser-form', function(e) {
        alert("press OK add");
        page="admin";
        e.preventDefault();
        checkFields();       
    })
})
$(function(){
    $(document).on('submit','.js-registration-form',function(e){
        page="registration";
        alert("press OK reg");
        e.preventDefault();
        checkFields(); 
    })
})
